package com.lcxw.hxzbapi.dao.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.lcxw.hxzbapi.entity.pojo.YwpdGiftBag;

@Mapper
public interface GiftbagMapper {

    @Delete({
        "delete from ywpd_giftbag",
        "where `id` = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    //背包中插入一条记录
    @Insert({
        "insert into ywpd_giftbag (`giftId`, `giftNum`, ",
        "`userId`, `addTime`, ",
        "`expire`)",
        "values (#{giftId}, #{giftNum}, ",
        "#{userId}, #{addTime}, ",
        "#{expire})"
    })
    int insert(YwpdGiftBag giftBag);


    //按照id查询背包中的记录
    @Select({
        "select",
        "`id`, `giftId`, `giftNum`, `userId`, `addTime`, `expire`",
        "from ywpd_giftbag",
        "where `id` = #{id,jdbcType=INTEGER}"
    })
    YwpdGiftBag selectByPrimaryKey(Integer id);

    //通过userId查询所有记录
    @Select("SELECT * FROM ywpd_giftbag WHERE userId=#{userId}")
    List<YwpdGiftBag> selectByUserId(long userId);

    //通过userId和giftId查询
    @Select("SELECT * FROM ywpd_giftbag WHERE userId=#{userId} AND giftId=#{giftId}")
    YwpdGiftBag selectByUserIdAndGiftId(@Param("userId") long userId, @Param("giftId") int giftId);

    //通过userId和giftId查询礼物数量，giftNum
    @Select("SELECT giftNum from ywpd_giftbag WHERE userId=#{userId} AND giftId=#{giftId}")
    int selectGiftNumByUserIdAndGiftId(@Param("userId") long userId, @Param("giftId") int giftId);

    //更新礼物数量，通过userId和giftId
    @Update("update ywpd_giftbag set giftNum = giftNum + #{giftNum} where userId=#{userId} AND giftId=#{giftId}")
    int updateGiftNum(@Param("userId") long userId, @Param("giftId") int giftId,
        @Param("giftNum") int giftNum);

    //更新一整条数据
    @Update({
        "update ywpd_giftbag",
        "set `giftId` = #{giftId},",
        "`giftNum` = #{giftNum},",
        "`userId` = #{userId},",
        "`addTime` = #{addTime},",
        "`expire` = #{expire}",
        "where `id` = #{id}"
    })
    int updateByPrimaryKey(YwpdGiftBag ywpdGiftBag);
}
